#!/usr/bin/perl -w

#         FILE: dbEvent.pl
#       AUTHOR:	RCP y JAE
#         DATE:	15/02/01

#  DESCRIPTION: Funciones para manejar el envio de notificaciones de eventos (Suscripciones)

require ("dbData.pl");
require ("dbUser.pl");

sub MakeMail
{


 local ($toEmail, $fromEmail, $msgEmail, $subEmail) = @_;
# print " use mail . $toEmail, $fromEmail, $msgEmail, $subEmail ";
 use Mail::Sendmail;  

 %mailToSend = 	(
		From 	=> $fromEmail,
		Bcc  	=> $toEmail,
		Message	=> $msgEmail,
		Subject => $subEmail,
		);
#print "\n<br> from $fromEmail; bcc $toEmail; mes: $msgEmail; subject $susubEmail. ";

  if (sendmail(%mailToSend))
  {
    $txt= 'okMail';
    $val = 1
  }
  else
  {
     $txt= 'errorMail';
     $val = 0;
  }

 return $val;
 
}


sub notifyDocOn
{
 
 local ($dbName, $idAuthor) = @_;
 
 local($T, $R);

 #Abrir la Base de datos
 $dbh = &openDB($dbName);

 #Recuperar datos de la base de datos
 $sql = "SELECT * FROM AuthorPreference WHERE idAuthor = $idAuthor;";
 
 #Ejecutar sql
 $sth =  $dbh->prepare($sql);
 $sth->execute || die "No se puede ejecutar la orden SQL";

 
 $row = $sth->fetchrow_hashref;
 $T   = $row->{sPropDoc};

 if ($T eq 1) 
 {
   $R = $row->{notifyDays};
 }
 else
 {
   $R = -1;
 }
 
 #desconectar base de datos
 $dbh->disconnect;

 return $R;

}




sub getProposal
{

 local($dbName, $date) = @_;

 local(%T);

 #Abrir la Base de datos
 $dbh = &openDB($dbName);

 #Recuperar datos de la base de datos
 $sql = "SELECT * FROM Proposal WHERE dayEnd>='$date' AND deleteP=0 AND associateDes=0 ORDER BY idAuthor;";  

 #Ejecutar sql
 $sth =  $dbh->prepare($sql);
 $sth->execute || die "No se puede ejecutar la orden SQL";

 $cont = 0;
 while($row = $sth->fetchrow_hashref)
 {
  $cont++;
  $T{idAuthor.$cont} = $row->{idAuthor};
  $T{idTopic.$cont}  = $row->{idTopic};
  $T{dayEnd.$cont}   = $row->{dayEnd};
  $T{'comment'.$cont}  = $row->{comment};
 }
 
 $T{numRows} = $cont;
 
 #desconectar base de datos
 $dbh->disconnect;

 return %T;
}



sub getNameTopic
{

 local ($dbName, $idTopic, $idStruct) = @_;
 local ($dbh);
 #Abrir la Base de datos
 $dbh = &openDB($dbName);

 #Recuperar datos de la base de datos
 if ($idStruct > 0)
 {
   $sql = "SELECT * FROM Struct WHERE idTopic = $idTopic";
 }
 else
 {
   $sql = "SELECT * FROM Topic  WHERE idTopic = $idTopic";
 }
 
 #Ejecutar sql
 $sth =  $dbh->prepare($sql);
 $sth->execute || die "No se puede ejecutar la orden SQL";

 $row = $sth->fetchrow_hashref;
 $nameTopic = $row->{nameTopic};
  	
 #desconectar base de datos
 $dbh->commit;
 
 return($nameTopic);
}


sub getListTo 
{
 local($dbName, $Opt, $idTopic, $idStruct) = @_;
 local($T);

 $T = '';

 #Se obtiene la fase actual
 $phase = &get_phase($dbName);

 if ($phase == 0)
 {
     $sql_struct = " and Suscriptions.idStruct = $idStruct";
 }
 if ($phase == 1)
 {
     $sql_struct = "";
 }


 #Abrir la Base de datos
 $dbh = &openDB($dbName);

 #ver a quienes afecta el mandar mensaje
 $criterio = &modifyTo ($dbName);

 #Recuperar datos de la base de datos

 $sql = "select DISTINCT (Author.email) from ((Author left join Suscriptions on Author.idAuthor = Suscriptions.idAuthor) left join AuthorPreference on Author.idAuthor = AuthorPreference.idAuthor) LEFT JOIN Historic ON Author.idAuthor= Historic.idAuthor where ((Suscriptions.$Opt = '1' and Suscriptions.idTopic = $idTopic) or AuthorPreference.suscriptionAll = '1') $criterio;";

 if ($Opt eq 'sVotStruct')
 {
 $sql = "select DISTINCT (Author.email) from ((Author left join Suscriptions on Author.idAuthor = Suscriptions.idAuthor) left join AuthorPreference on Author.idAuthor = AuthorPreference.idAuthor) LEFT JOIN Historic ON Author.idAuthor= Historic.idAuthor where ((Suscriptions.$Opt = '1' and Suscriptions.idStruct = $idStruct) or AuthorPreference.suscriptionAll = '1') $criterio;";
 }

#print "\n<br> sql-event $sql ";

 #Ejecutar sql
 $sth =  $dbh->prepare($sql);
 $sth->execute || die "No se puede ejecutar la orden SQL";

 while($row = $sth->fetchrow_hashref)
 {
  $T = $row->{email} . " $T";
 }

# print "// $T";

 #desconectar base de datos
 $dbh->disconnect;

 return $T;
}


sub whoisAuthor
{

 local($dbName, $idDes) = @_;
 local($T);

 #Abrir la Base de datos
 $dbh = &openDB($dbName);

 #Recuperar datos de la base de datos
 $sql = "SELECT * FROM Description WHERE idDescription = $idDes;";
 
 #Ejecutar sql
 $sth =  $dbh->prepare($sql);
 $sth->execute || die "No se puede ejecutar la orden SQL";

 $row = $sth->fetchrow_hashref;
 
 $T = $row->{idAuthor};
 
 
 #desconectar base de datos
 $dbh->disconnect;

 return $T;
 
}


sub getListGTo 
{
 local($dbName, $Opt, $idDes) = @_;
 local($T);
 #print " dentro ";
 $Autor = &whoisAuthor($dbName, $idDes)  if (($Opt eq 'sVotDoc') || ($Opt eq 'sVoteVersion')); 
 $Autor = $idDes if ($Opt eq 'sVoteNote');

 #Abrir la Base de datos
 $dbh = &openDB($dbName);
 #print " a cri ";
 $criterio = &modifyTo ($dbName);
 #print " criterio $criterio ";
 #Recuperar datos de la base de datos
 $sql = "SELECT DISTINCT (Author.email) FROM (AuthorPreference LEFT JOIN Author ON AuthorPreference.idAuthor = Author.idAuthor) LEFT JOIN Historic ON Author.idAuthor= Historic.idAuthor WHERE (AuthorPreference.$Opt = '1' or AuthorPreference.suscriptionAll = '1') $criterio;";
 #print " sql $sql ";
 if (($Opt eq 'sVoteVersion') || ($Opt eq 'sVoteNote'))
 {
 $sql = "SELECT DISTINCT (Author.email) FROM (AuthorPreference LEFT JOIN Author ON AuthorPreference.idAuthor = Author.idAuthor) LEFT JOIN Historic ON Author.idAuthor= Historic.idAuthor WHERE AuthorPreference.idAuthor = $Autor and (AuthorPreference.$Opt = '1' or AuthorPreference.suscriptionAll = '1') $criterio;";
 }

 if ($Opt eq 'sVotStr')
 {
 $sql = "SELECT DISTINCT (Author.email) FROM ((Author left join StructDes on  Author.idAuthor = StructDes.idAuthor ) LEFT JOIN AuthorPreference ON Author.idAuthor = AuthorPreference.idAuthor) LEFT JOIN Historic ON Author.idAuthor= Historic.idAuthor WHERE StructDes.idStruct = $idDes AND (AuthorPreference.$Opt = '1' or AuthorPreference.suscriptionAll = '1') $criterio;";
 }
 
 if ($Opt eq 'sVotDoc')
 {
 $sql = "SELECT DISTINCT (Author.email) FROM ((Author left join Description on Author.idAuthor = Description.idAuthor)  LEFT JOIN AuthorPreference on Author.idAuthor = AuthorPreference.idAuthor) LEFT JOIN Historic ON Author.idAuthor= Historic.idAuthor WHERE Description.idDescription = $idDes AND (AuthorPreference.$Opt = '1' or AuthorPreference.suscriptionAll = '1') $criterio;";
 }

 if ($Opt eq 'sPropDoc')
 {
     $sql = "SELECT DISTINCT (Author.email) FROM Author LEFT JOIN Historic ON Author.idAuthor= Historic.idAuthor WHERE idAuthor = $idDes $criterio;";
 }
# print " sql $sql ";
 #Ejecutar sql
 $sth =  $dbh->prepare($sql);
 $sth->execute || die "No se puede ejecutar la orden SQL";

 while($row = $sth->fetchrow_hashref)
 {
  $T = $row->{email} . " $T";
 }

#print "\n<br> sql-Gevent $sql ";

 #desconectar base de datos
 $dbh->disconnect;

 return $T;
}


sub getdes
{

 local($dbName, $idDes) = @_;
 
 #Abrir la Base de datos
 $dbh = &openDB($dbName);

 #Recuperar datos de la base de datos
 $sql = "SELECT * FROM Description WHERE idDescription = $idDes;";

 #Ejecutar sql
 $sth =  $dbh->prepare($sql);
 $sth->execute || die "No se puede ejecutar la orden SQL";

 $row = $sth->fetchrow_hashref;
 
 $date   = $row->{dayD};
 ($iyear, $imonth, $iday) = split (/-/, $date);
 ($iday, $resto) 	  = split (/ /, $iday);
 $date   = $iday . '/' . $imonth . '/' . $iyear; 
 $author = $row->{idAuthor}; 

 #desconectar base de datos
 $dbh->disconnect;

 %Temp  = &get_data($dbName, "Author", "idAuthor", "$author");
 $nameAuthor = $Temp{nameAuthor.1};

 $result = $nameAuthor . "[$date]";

 return $result;

}


sub whosFather
{

 local ($dbName, $idTopic) = @_;

 #Abrir la Base de datos
 $dbh = &openDB($dbName);

 #Recuperar datos de la base de datos
 $sql = "SELECT * FROM SubTopic WHERE idTopic = $idTopic;";

 #Ejecutar sql
 $sth =  $dbh->prepare($sql);
 $sth->execute || die "No se puede ejecutar la orden SQL";

 $row = $sth->fetchrow_hashref;
 
 #desconectar base de datos
 $dbh->disconnect;

 return $row->{idSuperTopic};
}


sub getValor
{
 local($Accion, $valor, %Tabla) = @_;
 
 if ($Accion == 4)
 {
  	$R = " $valor";
 }
 if ($Accion == 6)
 {
  	$R = " $Tabla{valor6_1}"   	if ($valor == 1);
	$R = " $Tabla{valor6__1}"  	if ($valor == -1);
 }
 if ($Accion == 9)
 {
  	$R = " $Tabla{valor9_1}"   	if ($valor == 1);
	$R = " $Tabla{valor9__1}" 	if ($valor == -1);
 } 
 if (($Accion == 22) || ($Accion == 11) || ($Accion == 27))
 {
        $R = " $Tabla{valor1}"      	if ($valor == 1);
        $R = " $Tabla{valor0}"      	if ($valor == 0);
        $R = " $Tabla{valor_1}"    	if ($valor == -1);
 }
 if (($Accion == 25) || ($Accion == 31))
 {
        $R = " $Tabla{valor1}" 		if ($valor == 1);
        $R = " $Tabla{valor_1}"		if ($valor == -1);
 }

 return $R;
}


sub getTabla
{

 local($dbName, $Accion) = @_;
 local($dbh, %Datos);
  
 #Abrir la Base de datos
 ($dbLanguage, $IMG) = &get_language($dbName);

 $dbh = &openDB($dbLanguage);

 #Recuperar datos de la base de datos
 $sql = "SELECT * FROM TxtEvent WHERE accion = 0 OR accion = $Accion;";

 #Ejecutar sql
 $sth =  $dbh->prepare($sql);
 $sth->execute || die "No se puede ejecutar la orden SQL";

 while($row = $sth->fetchrow_hashref)
 {
   $campo 	  = $row->{field};
   $Datos{$campo} = $row->{txt};
 }
 
 $sql = "SELECT * FROM NotifyOptions WHERE accion = $Accion;";

 $sth =  $dbh->prepare($sql);
 $sth->execute || die "No se puede ejecutar la orden SQL";

 $row = $sth->fetchrow_hashref;

 $Datos{opcion} = $row->{opcion};

 #desconectar base de datos
 $dbh->commit;

 return %Datos;
}


# -----------------------------FUNCION DE NOTIFICACION DE EVENTOS GENERALES ------------------------------------- 

sub notificaVersion 
{
    local ($dbName, $idTopic, $idAuthor, $idDescription, $idStruct, $nameTopic) = @_;
    
    # Formacion de ToMail

    %expert = &get_expertsOnTopic($dbName, $idTopic); 
    if ($expert{numRows} == 0) 
    {
	%expert = &get_whoVotDoc($dbName, $idTopic);
    }
    foreach $i(1..$expert{numRows})
    {
	if (($expert{idAuthor.$i} != $idAuthor))
	{
	    $toEmail = $toEmail . $expert{eMail.$i} . ";";
	}
    }

    # Formacion del FromMail

    %GD = &get_data ($dbName, "GeneralData", "idGeneralData", 1);   
    $kcEmail = $GD{'KcEmail'.1};

    # Formacion del mensaje y subject

    %Table = &getTabla($dbName, 43);

    %Temp  = &get_data($dbName, "Author", "idAuthor", "$idAuthor"); 
    $nameAuthor = $Temp{nameAuthor.1};
    $emailAuthor = $Temp{eMail.1};
    $Text = "$Table{Hola}\n\n" . $nameAuthor;
    
    %Temp = &get_data($dbName, "Description", "idDescription", "$idDescription");
    $des = $nameAuthor .  "[ $Temp{\"dayD\".1} ]"; 
    $Text = $Text . $Table{aviso} . " $des $Table{expuesto} $nameTopic. ";

    $phase = &get_phase($dbName);
    %Temp = &get_data($dbName, "Phase", "idPhase", "$phase");
    $tMax = $Temp{tMaxVersion.1};
    $today = &get_date();
    $vdate = &date2value($today);
    $today = &value2date($vdate + $tMax);
    $Text = $Text . $Table{pedimos} . " $today\n\n";

    $Text = $Text . "$Table{recordamos}\n\n";

    $Text = $Text . "      $Table{grado1}.\n";

    $Text = $Text . "      $Table{grado2}.\n\n";
    
    $path = "http://knowcat.ii.uam.es/3.0/";   ## ESTO??
    $path = $path . "inter.pl?b=$dbName&t=$idTopic&s=$idStruct&d=$idDescription";
    $Text = $Text . "$Table{url} $path";

    $Text = $Text . "\n\n" . $Table{gracias};

    &MakeMail ($toEmail, $kcEmail, $Text,  $Table{notificacion});

    $toEmail2 = $emailAuthor;

    %Table = &getTabla($dbName, 44);

    $Text = "$Table{Hola}\n\n";
    $Text = $Text . $Table{aviso} . " $des ";
    $Text = $Text . $Table{hasido} . " $nameTopic ";
    $Text = $Text . $Table{yse} . " $today ";
    $Text = $Text . "\n\n" . $Table{gracias};

    &MakeMail ($toEmail2, $kcEmail, $Text,  $Table{notificacion});

    return ($toEmail);
  
}

## CON ESTA ESTA RR ##
sub rememberVoteVersion 
{
    local ($dbName, $idTopic, $idAuthor, $idDescription, $idStruct, $nameTopic) = @_;
    
    # Formacion de ToMail
    %expert = &get_expertsOnTopic($dbName, $idTopic); 
    if ($expert{numRows} == 0) 
    {
	%expert = &get_whoVotDoc($dbName, $idTopic);
    }
    foreach $i(1..$expert{numRows})
    {
	if (($expert{idAuthor.$i} != $idAuthor))
	{
	    $toEmail = $toEmail . $expert{eMail.$i} . ";";
	}
    }

    # Formacion del FromMail

    %GD = &get_data ($dbName, "GeneralData", "idGeneralData", 1);   
    $kcEmail = $GD{'KcEmail'.1};

    # Formacion del mensaje y subject


    %Table = &getTabla($dbName, 43);

    %Temp  = &get_data($dbName, "Author", "idAuthor", "$idAuthor");

    $nameAuthor = $Temp{nameAuthor.1};
    $emailAuthor = $Temp{eMail.1};
    $Text = "$Table{Hola}\n\n" . $Table{reaviso} . " " . $nameAuthor;
    
    %Temp = &get_data($dbName, "Description", "idDescription", "$idDescription");
    $des = $nameAuthor .  "[ $Temp{\"dayD\".1} ]"; 
    $Text = $Text . $Table{aviso} . " $des $Table{expuesto} $nameTopic. ";

    $phase = &get_phase($dbName);
    %Temp = &get_data($dbName, "Phase", "idPhase", "$phase");
    $tMax = $Temp{tMaxVersion.1};
    $today = &get_date();
    $vdate = &date2value($today);
    $today = &value2date($vdate + $tMax);
    $Text = $Text . $Table{pedimos} . " $today\n\n";

    $Text = $Text . "$Table{recordamos}\n\n";

    $Text = $Text . "      $Table{grado1}.\n";

    $Text = $Text . "      $Table{grado2}.\n\n";
    
    $path = "http://knowcat.ii.uam.es/3.0/";
    $path = $path . "inter.pl?b=$dbName&t=$idTopic&s=$idStruct&d=$idDescription";
    $Text = $Text . "$Table{url} $path";

    $Text = $Text . "\n\n" . $Table{gracias};

    &MakeMail ($toEmail, $kcEmail, $Text,  $Table{notificacion});

    return ($toEmail);
}

sub deleteVersion 
{

    $toEmail2 = $emailAuthor;

    %Table = &getTabla($dbName, 44);

    $Text = "$Table{Hola}\n\n";
    $Text = $Text . $Table{aviso} . " $des ";
    $Text = $Text . $Table{hasido} . " $nameTopic ";
    $Text = $Text . $Table{yse} . " $today ";
    $Text = $Text . "\n\n" . $Table{gracias};
print " &MakeMail ($toEmail2, $kcEmail, $Text,  $Table{notificacion}); ";

#    &MakeMail ($toEmail2, $kcEmail, $Text,  $Table{notificacion});

}


sub notifyEventG
{
 
 local ($dbName, %Datos) = @_;
 
 local ($NombreTema, $L, $Text, $Accion);

 %GD = &get_data ($dbName, "GeneralData", "idGeneralData", 1);
 $kcEmail = $GD{'KcEmail'.1};

 #Fijando la accion que se va a notificar
 $Accion = $Datos{Accion};
# print "antes tabla ";
 #Traer campos y valores de texto de notificacion - Considera el idioma -
 %Tabla = &getTabla($dbName, $Accion); 
# print " despues tabla ";
 $Text = $Tabla{Hola} . "\n\n";

 #Traer el nombre del usuario y agregarlo al texto
 %Temp  = &get_data($dbName, "Author", "idAuthor", "$Datos{Autor}");
 $Text =  $Text . $Temp{nameAuthor.1}  					 if ($Accion != 30);

 #Agregar el texto de la accion
 $Text = $Text . $Tabla{campo1};

 #Agregar cambio de linea si se necesita (accion 22)
 $Text = $Text . "\n\n"							 if ($Accion == 22);

 $Text = $Text . " " . $Datos{Estructura} . $Tabla{campo2}               if ($Accion == 27);

 $Text = $Text . " " . &getValor ($Datos{Accion}, $Datos{Valor}, %Tabla) if (($Accion == 25) || ($Accion == 31) || ($Accion == 27));
 $Text = $Text . " " . $Datos{Estructura}				 if ($Accion == 26);
 $Text = $Text . " " . &getdes($dbName, $Datos{Descripcion})	     if (($Accion == 28) || ($Accion == 32));
 $Text = $Text . " " . $Datos{Dias} . $Tabla{campo1_1}		     if ($Accion == 30);

 if ($Accion == 31)
 {
     $Text = $Text . " $Tabla{anotacion} $Datos{titulo}";
     $Text = $Text . " $Tabla{descripcion} " . &getdes($dbName, $Datos{Descripcion});
 }
     
 if (($Accion == 28) || ($Accion >= 30))
 {
  	$NombreTema = &getNameTopic ($dbName, $Datos{Tema}, $Datos{Estructura});
 	$Text = $Text . " $Tabla{campo3} " 		  . $NombreTema;
	$Text = $Text . " $Tabla{campo4} $Tabla{campo5} " . $Datos{Estructura}  if (($Datos{Estructura} != -1) && ($Accion != 30));
	$Text = $Text . " $Tabla{campo2} " 		  . $Datos{Valor}	if ($Accion == 28);
 }

  if ($Accion == 32)
 {
#     print " hola ";
     $Text = $Text . " $Tabla{siguientes_valores}\n\n\t";
     $Text = $Text . "$Tabla{grado_continuidad} $Datos{Valor1}\n\t";
     $Text = $Text . "$Tabla{grado_mejora} $Datos{Valor2}";
 }

 if ($Accion == 22)
 {
	for ($i = 1; $i <= $Datos{Cuantos}; $i++)
	{
	   %Temp  = &get_data($dbName, "Author", "idAuthor", "$Datos{Quien.$i}");
  	   $Valor = &getValor($Datos{Accion}, $Datos{Valor.$i}, %Tabla);
	   $Text = $Text . "$Temp{nameAuthor.1}\t$Valor\n";
	}
 }

 if ($Datos{Quien} ne "")
 {
 	#Traer el nombre del usuario
 	%Temp  = &get_data($dbName, "Author", "idAuthor", "$Datos{Quien}");
 	$Text = $Text . " $Temp{nameAuthor.1}";
 }
# print " hyh ";
 #Agregar comentario
 $Text = $Text . "\n\n$Tabla{campo6}\n" . $Datos{Comentario}  if (($Datos{Comentario} ne "") && (($Datos{Accion} == 24) || ($Datos{Accion} == 26)));

 #Poner fecha y saludo
 $Text = $Text . "\n\n$Tabla{campo7} " . $Datos{Fecha};
 $Text = $Text . "\n\n" . $Tabla{gracias};

 $Extra = $Datos{nAutor}                                     if ($Accion == 31);
 $Extra = $Datos{Descripcion}				     if (($Accion == 28) || ($Accion == 32));
# print " aqui ";
 $Extra = $Datos{Autor}					     if ($Accion == 30);
 $Extra = $Datos{Estructura}                                 if ($Accion == 27);
# print " gd ";
 %GD = &get_data ($dbName, "GeneralData", "idGeneralData", 1);   
# print " des gd ";
 $kcEmail = $GD{'KcEmail'.1};

 $Text = $Text . "\n__________________________________________________________________________________________\n" . $Tabla{pd} . " ". $kcEmail;

 $Subject = $Tabla{notificacion};
# print " lis t ($dbName, $Tabla{opcion}, $Extra ";
 $L = &getListGTo ($dbName, $Tabla{opcion}, $Extra);
# print " des lis ($dbName, $Tabla{opcion}, $Extra";
# print " mail $L, $kcEmail, $Text, $Subject ";
 if ($L ne '')
 {
     &MakeMail($L, $kcEmail, $Text, $Subject);
 }
 return 1;

}

# -----------------------------FUNCION DE NOTIFICACION DE EVENTOS NO GENERALES ------------------------------------- 


sub notifyEvent
{
 local ($dbName, %Datos) = @_;

 local ($NombreTema, $L, $Text, $Accion);

 %GD = &get_data ($dbName, "GeneralData", "idGeneralData", 1);
 $kcEmail = $GD{'KcEmail'.1};

 $Accion = $Datos{Accion};

 #Traer nombre del usuario
 if (($Accion != 7) && ($Accion != 10))
 {
   %Temp  = &get_data($dbName, "Author", "idAuthor", "$Datos{Autor}");
   $Text = $Text . $Temp{nameAuthor.1};
 }

 #Traer el tema padre en el caso que se necesite
 $Datos{Tema} = &whosFather($dbName, $Datos{TemaHijo}) 				if (($Datos{Accion} == 6) || ($Datos{Accion} == 7));

 #Traer campos y valores de texto de notificacion - Considera el idioma -
 %Tabla = &getTabla($dbName, $Accion);

 $Text = $Tabla{Hola} .  "\n\n";

 $Text = $Text . $Temp{nameAuthor.1} 						if (($Accion != 7) && ($Accion != 10));

 $Text = $Text . $Tabla{campo1};

 $Text = $Text . " $Datos{Fecha}"						if ($Accion == 1);
 $Text = $Text . " $Datos{URL}"							if ($Accion == 2);
 $Text = $Text . " $Datos{Path}"						if ($Accion == 3);
 $Text = $Text . " " . &getdes($dbName, $Datos{Descripcion})			if ($Accion == 4);
 $Text = $Text . " $Datos{TemaHijo}"						if ($Accion == 5);

 if ($Accion == 12)
 {
     $Text = $Text . " $Datos{Titulo} $Tabla{documento} " . &getdes($dbName, $Datos{Descripcion});
 }

 $Valor = &getValor($Accion, $Datos{Valor}, %Tabla)                             if ($Accion != 12);

 $Text = $Text . $Tabla{campo2}							if (($Accion == 4) || ($Accion == 11));

 $Text = $Text . $Valor                                                         if ($Accion != 12);

 $NombreTema = &getNameTopic ($dbName, $Datos{TemaHijo}, $Datos{Estructura})	if (($Accion == 6) || ($Accion == 7));

 $Text = $Text . " $NombreTema "                                                if (($Accion != 12) && ($Accion > 2) && ($Accion != 4));

 $Text = $Text . "$Tabla{campo3}" 						if ($Accion < 8);

 $Text = $Text . "$Tabla{expuesto}"                                             if ($Accion == 12);

 $NombreTema = "";

 $NombreTema = &getNameTopic ($dbName, $Datos{Tema}, $Datos{Estructura})	if ($Accion != 11);

 $Text = $Text . " $NombreTema "                                                if ($Accion != 11);

 $Text = $Text . $Tabla{campo4}							if (($Datos{Estructura} > 0) && ($Datos{Accion} != 11));

 $Text = $Text . $Tabla{campo5}	. " $Datos{Estructura}"				if (($Accion >= 11) || ($Datos{Estructura} > 0));

 $Text = $Text . "\n\n$Tabla{campo6}\n" . $Datos{Comentario} 			if (($Accion == 1) && ($Datos{Comentario} != ""));


 #Poner fecha
 ($a, $m, $d)   = split(/-/, $Datos{Fecha});
 $Datos{Fecha} = $d . '/' . $m . '/' . $a;

 $Text = $Text . "\n\n$Tabla{campo7} " . $Datos{Fecha};

 $Text = $Text . "\n\n" . $Tabla{gracias};

 %GD = &get_data ($dbName, "GeneralData", "idGeneralData", 1);   
 $kcEmail = $GD{'KcEmail'.1};

 $Text = $Text . "\n__________________________________________________________________________________________\n" . $Tabla{pd} . " ". $kcEmail;

 $Subject = $Tabla{notificacion};

 $L = &getListTo ($dbName, $Tabla{opcion}, $Datos{Tema}, $Datos{Estructura});

  if ($L ne '')
  {

     &MakeMail($L, $kcEmail, $Text, $Subject);
  }

 return 1;
}

return 1;
